/*
 * Copyright (C) 2013 Chen Hui <calmer91@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.harmonyos.fishbun.slice;

import com.harmonyos.fishbun.ImageAdapter;
import com.harmonyos.fishbun.ImageController;
import com.harmonyos.fishbun.ResourceTable;
import com.sangcomz.fishbun.BaseAbilitySlice;
import com.sangcomz.fishbun.FishBun;
import com.sangcomz.fishbun.adapter.image.impl.GlideAdapter;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;
import ohos.utils.net.Uri;

import java.util.ArrayList;

public class SubAbilitySlice extends BaseAbilitySlice {

    private ArrayList<Uri> path = new ArrayList<>();
    private ImageAdapter imageAdapter;

    public static SubAbilitySlice newInstance() {
        return new SubAbilitySlice();
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_slice_sub);

        ListContainer recyclerview = (ListContainer) findComponentById(ResourceTable.Id_recyclerview);
        Image img_main = (Image) findComponentById(ResourceTable.Id_img_main);
        imageAdapter = new ImageAdapter(this, new ImageController(img_main, this), path);
        recyclerview.setItemProvider(imageAdapter);


        findComponentById(ResourceTable.Id_btn_add_images).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                FishBun.with(getAbility())
                        .setImageAdapter(new GlideAdapter())
                        .setMaxCount(10)
                        .setActionBarColor(Color.getIntColor("#3F51B5"), Color.getIntColor("#303F9F"))
                        .setCamera(true)
                        .setSelectedImages(path)
                        .startAlbum();
            }
        });
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if (requestCode == FishBun.FISHBUN_REQUEST_CODE && resultCode == RESULT_OK) {
            path = resultData.getSequenceableArrayListParam(FishBun.INTENT_PATH);
            imageAdapter.changePath(path);
        }
    }
}
